package com.soft.mainsite.module.web.cms.service.impl;

import com.google.common.collect.Maps;
import com.soft.mainsite.common.constant.CmsConst;
import com.soft.mainsite.common.utils.StrUtil;
import com.soft.mainsite.component.beetl.html.BeetlHtmlUtil;
import com.soft.mainsite.module.staticstrategy.service.TemplateManagerService;
import com.soft.mainsite.module.web.cms.service.*;
import com.soft.mainsite.mybatis.mapper.TCmsContentMapper;
import com.soft.mainsite.mybatis.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * 页面静态化接口实现
 *
 * @author liupengtao
 * @date 2019/04/30
 */
@Service
public class HtmlStaticServiceImpl implements HtmlStaticService {

    @Autowired
    private SiteService siteService;

    @Autowired
    private BeetlHtmlUtil beetlHtmlUtil;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private TCmsContentMapper contentMapper;

    @Autowired
    private ModelService modelService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private TopicService topicService;

    private TemplateManagerService templateManagerService;

    /**
     *
     * @param siteId
     * @throws Exception
     */
    @Override
    public void index(Integer siteId) throws Exception {
        TCmsSite site = siteService.findById(siteId);
        Map sitInfo = Maps.newHashMap();
        sitInfo.put("site",site);
        sitInfo.put("title",site.getTitle());
        sitInfo.put("keyword",site.getKeyword());
        sitInfo.put("description",site.getDescription());
        String tpl = StrUtil.isBlank(site.getMobileTpl()) ? "default" : site.getMobileTpl();
        beetlHtmlUtil.create(request,siteId, CmsConst.INDEX_TPL, sitInfo, tpl, CmsConst.INDEX_TPL, false,CmsConst.TPL_TYPE_PC);
    }

    /**
     * 生成栏目页
     * @param siteId
     * @param categoryId
     * @param pageNumber
     * @param isPageList
     * @throws Exception
     */

    @Override
    public void category(Integer siteId,Long categoryId,Integer pageNumber,boolean isPageList) throws Exception {
        TCmsSite site = siteService.findById(siteId);
        TCmsCategory category = categoryService.findById(categoryId);
        Map attr = Maps.newHashMap();
        attr.put("title", category.getCategoryName());
        attr.put("keyword", site.getKeyword());
        attr.put("site", site);
        attr.put("description", site.getDescription());
        attr.put("category", category);

        if (!isPageList) {
            beetlHtmlUtil.create(request, siteId, category.getAlias(), attr, (StrUtil.isBlank(site.getMobileTpl()) ? "default" : site.getMobileTpl()), StrUtil.isBlank(category.getIndexTpl()) ? CmsConst.CATEGORY_INDEX_TPL : category.getIndexTpl(), false,CmsConst.TPL_TYPE_PC);
        }else {
            beetlHtmlUtil.create(request, siteId, category.getAlias() + File.separator+"index_" + pageNumber, attr, (StrUtil.isBlank(site.getMobileTpl()) ? "default" : site.getMobileTpl()), StrUtil.isBlank(category.getListTpl()) ? CmsConst.CATEGORY_LIST_TPL : category.getListTpl(), false,CmsConst.TPL_TYPE_PC);
        }
    }

    @Override
    public void categoryList(Integer siteId, Long categoryId, Integer pageNumber, boolean isPageList) throws Exception {

    }

    /**
     * 生成内容页
     * @param tCmsContent
     * @throws Exception
     */
    @Override
    public void content(TCmsContent tCmsContent) {
        TCmsSite site = siteService.findById(tCmsContent.getSiteId());
        TCmsCategory category = categoryService.findById(tCmsContent.getCategoryId());
        TCmsModel contentModel = modelService.findById(category.getModelId());
        Map content = contentMapper.selectByContentIdAndTableName(tCmsContent.getContentId(),contentModel.getTableName());
        Boolean isCommon = category.getIsCommon();

        Map attr = Maps.newHashMap();
        attr.put("title",category);
        attr.put("keyword",content.get("keywords"));
        attr.put("description",content.get("description"));
        attr.put("site",site);
        attr.put("category",category);
        attr.put("content",content);
        
        try {
            beetlHtmlUtil.create(request,tCmsContent.getSiteId(), category.getAlias()+File.separator+tCmsContent.getContentId(), attr, (StrUtil.isBlank(site.getMobileTpl()) ? "default" : site.getMobileTpl()), StrUtil.isBlank(category.getContentTpl()) ? CmsConst.CATEGORY_LIST_TPL: category.getContentTpl(), isCommon,CmsConst.TPL_TYPE_PC);
            beetlHtmlUtil.create(request,tCmsContent.getSiteId(), category.getAlias()+File.separator+tCmsContent.getContentId(), attr, (StrUtil.isBlank(site.getMobileTpl()) ? "default" : site.getMobileTpl()), StrUtil.isBlank(category.getListTpl()) ? CmsConst.CATEGORY_LIST_TPL: category.getListTpl(), isCommon,CmsConst.TPL_TYPE_PC);
            beetlHtmlUtil.create(request,tCmsContent.getSiteId(), category.getAlias()+File.separator+tCmsContent.getContentId(), attr, (StrUtil.isBlank(site.getMobileTpl()) ? "default" : site.getMobileTpl()), StrUtil.isBlank(category.getIndexTpl()) ? CmsConst.CATEGORY_LIST_TPL: category.getIndexTpl(), isCommon,CmsConst.TPL_TYPE_PC);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void topic(Integer siteId) throws Exception {
        TCmsSite site = siteService.findById(siteId);
        List<TCmsTopic> topicList = topicService.findAll();
        for(TCmsTopic topic :topicList) {
            Map attr = Maps.newHashMap();
            attr.put("title", topic.getTopicName());
            attr.put("keyword", topic.getKeywords());
            attr.put("description", topic.getDescription());
            attr.put("site", site);
            attr.put("topic", topic);
            beetlHtmlUtil.create(request,siteId,"topic"+File.separator+topic.getTopicId(),attr,(StrUtil.isBlank(site.getMobileTpl()) ? "default" : site.getTemplate()), !StrUtil.isBlank(topic.getTopicTpl()) ? topic.getTopicTpl() : CmsConst.TOPIC_TPL, false,CmsConst.TPL_TYPE_PC);
        }
    }

    private Map<String, Object>  builderSiteInfo(){
        return null;
    }

}
